package com.ms.tank.game;


import com.ms.tank.abstractfactory.*;
import com.ms.tank.common.PropertyMgr;
import com.ms.tank.enums.Dir;
import com.ms.tank.enums.Group;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 坦克主程序
 * @Date 2021/2/28 19:45:21
 * @Author mashuai
 */
public class TankFrame extends Frame {

    public Tank myTank = new Tank(200, 500, Dir.UP, Group.GOOD, this);
    //子弹列表
    public List<BaseBullet> bullets = new ArrayList<>();
    //敌方坦克
    public List<BaseTank> tanks = new ArrayList<>();
    //爆炸
    public List<BaseExplode> explodes = new ArrayList<>();
    //工厂
    public GameFactory gameFactory = new DefaultFactory() ;
    //游戏框宽高
    public static final int GAME_WIDTH = Integer.parseInt(PropertyMgr.get("gameWidth").toString());
    public static final int GAME_HEIGHT = Integer.parseInt(PropertyMgr.get("gameHeight").toString());
    public TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setVisible(true);
        setResizable(false);
        setTitle("tank game");

        //添加键盘监听
        this.addKeyListener(new MykeyListener());

        //添加window监听器
        addWindowListener(new WindowAdapter() {
            //window事件触发
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //加载图片
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color color = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(color);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);

    }

    @Override
    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹数量:" + bullets.size(), 10, 60);
        g.drawString("敌人坦克数量:" + tanks.size(), 10, 75);
        g.drawString("爆炸数量:" + explodes.size(), 10, 90);
        g.setColor(color);

        //调用坦克类自己渲染图画
        myTank.paint(g);

        //调用子弹集渲染图画 (多个子弹)
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
        //调用坦克类自己渲染图画(敌方坦克)
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        //调用爆炸类自己渲染图画(敌方坦克)
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }
        //碰撞检查
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }
    }

    class MykeyListener extends KeyAdapter {

        boolean bl = false;
        boolean bu = false;
        boolean br = false;
        boolean bd = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bl = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    br = true;
                    break;
                case KeyEvent.VK_UP:
                    bu = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bd = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();
            //new Thread(()->new Audio("audio/tank_move.wav").play()).start();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bl = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    br = false;
                    break;
                case KeyEvent.VK_UP:
                    bu = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bd = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    //按ctrl开火
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {

            if (!bl && !bu && !br && !bd) {
                //如果坦克没有改变方向则静止
                myTank.setMoving(false);
            } else {
                //如果坦克改变方向则移动
                myTank.setMoving(true);
                if (bl) myTank.setDir(Dir.LEFT);
                if (br) myTank.setDir(Dir.RIGHT);
                if (bu) myTank.setDir(Dir.UP);
                if (bd) myTank.setDir(Dir.DOWN);
            }
            ;
        }
    }


}
